﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using DevExpress.Xpo;
using Aij1.Model;
using Aij1.BLL;
using System.Collections;
using Aij1.Utils;
using DevExpress.Data.Filtering;

namespace Aij1.Cms
{
    public class AdminPage : BasePage
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (CurrentUser == null && !DesignMode)
            {
#if DEBUG
                S_Employee user = XpoSession.FindObject<S_Employee>(CriteriaOperator.Parse("SysName='admin'"));
                if (user == null)
                {
                    user = Init1stAdmin();
                }
                AuthData.SetCurrentUser(user);
                CurrentUser = user;
#else
                int adminCount = (int)XpoSession.ExecuteScalar("SELECT COUNT(*) FROM S_Employee");
                if (adminCount > 0)
                {
                    Response.Redirect("AdminLogin.aspx");
                    Response.End();
                }
                else
                {
                    S_Employee user = Init1stAdmin();
                    AuthData.SetCurrentUser(user);
                }
#endif
            }
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            string themeSet = CommOp.ToStr(Request["Aij1_Theme"]);
            Theme = themeSet;
            if (String.IsNullOrEmpty(Theme)) Theme = "";
        }

        S_Employee Init1stAdmin()
        {
            S_Employee user = new S_Employee(XpoSession)
            {
                Password = CommOp.MD5("aij1"),
                SysName = "admin",
                TrueName = "管理员",
            };
            user.Save();
            return user;
        }
    }
}